package gadai;


import java.text.NumberFormat;
import javax.swing.UIManager;
import gadai.printer.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formGadai.java
 *
 * Created on 23 Okt 11, 20:41:50
 */
/**
 *
 * @author saiful nur budiman
 */
public class formGadai extends javax.swing.JFrame {
    double taksiran,ijaroh,stle,persen_upn,upn;
    double bobot,karat;
    double persen_ijaroh;
    int admin;
    double dev,kondisi;
    double up_normal,up_diambil=0;
    double diskon;
    double ijaroh_baru;
    NumberFormat currencyFormatter;
    private JDialog login;
    
    /** Creates new form formGadai */
    public formGadai() {
        initComponents();
        initialisasi();
        jTextField10.setVisible(false);
        bSave.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
   @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        deviasi = new javax.swing.JCheckBox();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        bSave = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        bHitung = new javax.swing.JButton();
        bCetak = new javax.swing.JButton();
        bGanti = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Gadai"));
        jPanel1.setPreferredSize(new java.awt.Dimension(509, 300));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("STLE :");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 36, -1, -1));

        jLabel2.setText("Bobot :");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 62, -1, -1));

        jLabel3.setText("Jenis Barang :");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 88, -1, -1));

        jLabel4.setText("Karat :");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 114, -1, -1));

        jLabel5.setText("Administrasi :");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 244, -1, -1));

        jLabel6.setText("Taksiran :");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 140, -1, -1));

        jLabel7.setText("Pembiayaan Normal :");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 166, -1, -1));

        jLabel9.setText("Ijaroh / 15 hari :");
        jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 218, -1, -1));

        jLabel10.setText("Pembiayaan Diambil :");
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 192, -1, -1));

        jTextField1.setEditable(false);
        jPanel1.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 33, 128, -1));

        jTextField2.setEditable(false);
        jPanel1.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(361, 31, 40, -1));
        jPanel1.add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 59, 82, -1));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Logam mulia", "Perhiasan" }));
        jPanel1.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 85, 128, -1));
        jPanel1.add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 111, 81, -1));

        jTextField5.setEditable(false);
        jPanel1.add(jTextField5, new org.netbeans.lib.awtextra.AbsoluteConstraints(134, 137, 129, -1));

        jTextField6.setEditable(false);
        jPanel1.add(jTextField6, new org.netbeans.lib.awtextra.AbsoluteConstraints(135, 163, 128, -1));
        jPanel1.add(jTextField7, new org.netbeans.lib.awtextra.AbsoluteConstraints(135, 189, 128, -1));

        jTextField8.setEditable(false);
        jPanel1.add(jTextField8, new org.netbeans.lib.awtextra.AbsoluteConstraints(135, 215, 128, -1));

        jLabel8.setText("Prosentase UPN :");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(273, 34, -1, -1));

        jTextField9.setEditable(false);
        jPanel1.add(jTextField9, new org.netbeans.lib.awtextra.AbsoluteConstraints(135, 241, 128, -1));

        deviasi.setBackground(new java.awt.Color(255, 255, 255));
        deviasi.setText("Deviasi");
        deviasi.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                deviasiItemStateChanged(evt);
            }
        });
        jPanel1.add(deviasi, new org.netbeans.lib.awtextra.AbsoluteConstraints(273, 82, -1, -1));

        jLabel11.setText("%");
        jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(405, 34, -1, -1));

        jLabel12.setText("gr");
        jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(226, 62, -1, -1));

        jLabel13.setText("karat");
        jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(225, 114, -1, -1));
        jPanel1.add(jTextField10, new org.netbeans.lib.awtextra.AbsoluteConstraints(334, 83, 128, -1));

        bSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gadai/image/save.png"))); // NOI18N
        bSave.setToolTipText("save");
        bSave.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        bSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bSaveMouseClicked(evt);
            }
        });
        jPanel1.add(bSave, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 33, -1, -1));

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setPreferredSize(new java.awt.Dimension(509, 0));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        bHitung.setMnemonic('h');
        bHitung.setText("Hitung");
        bHitung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bHitungActionPerformed(evt);
            }
        });
        jPanel2.add(bHitung, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 71, -1));

        bCetak.setMnemonic('c');
        bCetak.setText("Cetak");
        bCetak.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCetakActionPerformed(evt);
            }
        });
        jPanel2.add(bCetak, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 70, -1));

        bGanti.setMnemonic('g');
        bGanti.setText("Ganti");
        bGanti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGantiActionPerformed(evt);
            }
        });
        jPanel2.add(bGanti, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 10, 70, -1));

        jButton1.setText("Investasi >>");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 10, -1, -1));

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-497)/2, (screenSize.height-388)/2, 497, 388);
    }// </editor-fold>//GEN-END:initComponents

    private void bHitungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bHitungActionPerformed
        currencyFormatter = NumberFormat.getCurrencyInstance();
        stle=Double.parseDouble(jTextField1.getText());
        persen_upn=(Double.parseDouble(jTextField2.getText()))/100;
        karat=Double.parseDouble(jTextField4.getText());
        bobot=Double.parseDouble(jTextField3.getText());
        if((jComboBox1.getSelectedIndex()==0)&&(deviasi.isSelected()==false)){
            persen_ijaroh=(1.25/100)/2;
        }else if((jComboBox1.getSelectedIndex()==1)&&(deviasi.isSelected()==false)){
             persen_ijaroh=(1.5/100)/2;
        }else if(deviasi.isSelected()){
             persen_ijaroh=(Double.parseDouble(jTextField10.getText())/100)/2;
        }

        //perhitungan
        taksiran=((karat/24)*stle*bobot);
        upn=persen_upn*taksiran;
        
        //penentuan up_diambil, jika tidak diisi maka up_diambil=upn
        if(jTextField7.getText().equals("")){
            up_diambil=upn;
        }else{
            up_diambil=Double.parseDouble(jTextField7.getText());
        }
        
        //penentuan administrasi
        if(bobot>=1 && bobot<=100){
            admin=15000;
        }else if(bobot>100 && bobot<=200){
            admin=25000;
        }else if(bobot>200 && bobot<=300){
            admin=35000;
        }else if(bobot>300 && bobot<=400){
            admin=45000;
        }else{
            admin=50000;
        }
        ijaroh=persen_ijaroh*taksiran;
        kondisi=(up_diambil/upn)*100;
        
        //penentuan diskon 
        if(kondisi<=30){
            diskon=0.5;
        }else if(kondisi>30 && kondisi <=40){
            diskon=0.45;
        }else if(kondisi>40 && kondisi <=50){
            diskon=0.4;
        }else if(kondisi>50 && kondisi <=60){
            diskon=0.35;
        }else if(kondisi>60 && kondisi <=70){
            diskon=0.3;
        }else if(kondisi>70 && kondisi <=75){
            diskon=0.25;
        }else if(kondisi>75 && kondisi <=80){
            diskon=0.2;
        }else if(kondisi>80 && kondisi <=85){
            diskon=0.15;
        }else if(kondisi>85 && kondisi <=90){
            diskon=0.1;
        }else if(kondisi>90 && kondisi <=95){
            diskon=0.05;
        }
        ijaroh_baru=ijaroh-(ijaroh*diskon);
//        System.out.println(diskon);
//        System.out.println(ijaroh_baru);
        
//        System.out.println("STLE : "+stle);
//        System.out.println("Bobot : "+bobot);
//        System.out.println("Jenis : "+jComboBox1.getSelectedItem().toString());
//        System.out.println("Karat : "+karat);
//        System.out.println("Taksiran : "+taksiran);
//        System.out.println("Pembiayaan Normal : "+upn);
//        System.out.println("Pembiayaan Diambil : "+up_diambil);
//        System.out.println("Ijaroh/15 hari : "+ijaroh_baru);
//        System.out.println("Administrasi : "+admin);
//        System.out.println("Kondisi : "+kondisi);
        
        jTextField5.setText(currencyFormatter.format(taksiran).toString());
        jTextField6.setText(currencyFormatter.format(upn).toString());
        jTextField8.setText(currencyFormatter.format(ijaroh_baru).toString());
        jTextField9.setText(currencyFormatter.format(admin).toString());
        
    }//GEN-LAST:event_bHitungActionPerformed

    private void bGantiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGantiActionPerformed
        login initial=new login();
        String usr=JOptionPane.showInputDialog(null,"Usename ?");
        String pass=JOptionPane.showInputDialog(null,"Password ?");
        if(initial.init(usr, pass)==true){
            JOptionPane.showMessageDialog(this, "Welcome : "+usr,"Successfully !",JOptionPane.INFORMATION_MESSAGE);
            jTextField1.setEditable(true);
            jTextField2.setEditable(true);
            bSave.setVisible(true);
            enableAll(true);
        }else{
            JOptionPane.showMessageDialog(this, "Username atau Password anda salah !","Error !",JOptionPane.ERROR_MESSAGE);
        }       
    }//GEN-LAST:event_bGantiActionPerformed

    private void deviasiItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_deviasiItemStateChanged
        if(deviasi.isSelected()){
            jTextField10.setVisible(true);
        }else{
            jTextField10.setVisible(false);
        }
    }//GEN-LAST:event_deviasiItemStateChanged

    private void bCetakActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCetakActionPerformed
        PrintUtilities.printComponent(jPanel1);
    }//GEN-LAST:event_bCetakActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        formInvestasi investasi=new formInvestasi();
        investasi.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void bSaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bSaveMouseClicked
        save();
    }//GEN-LAST:event_bSaveMouseClicked

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
////            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
////                if ("Nimbus".equals(info.getName())) {
////                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
////                    break;
////                }
////            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(formGadai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(formGadai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(formGadai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(formGadai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new formGadai().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCetak;
    private javax.swing.JButton bGanti;
    public javax.swing.JButton bHitung;
    private javax.swing.JLabel bSave;
    private javax.swing.JCheckBox deviasi;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    // End of variables declaration//GEN-END:variables

    private void initialisasi() {
        FileGadai set=new FileGadai();
        jTextField1.setText(String.valueOf(set.getStle()));
        jTextField2.setText(String.valueOf(set.getPersenIjaroh()));
    }

    private void save() {
        Writer output=null;
        if(cek()==true){
            String input1=jTextField1.getText();
            String input2=jTextField2.getText();
            try{
                File file=new File("gadai");
                output=new BufferedWriter(new FileWriter(file));
                output.write(input1+";"+input2+";");
                output.close();
                
                jTextField1.setEditable(false);
                jTextField2.setEditable(false);
                bSave.setVisible(false);
                
                enableAll(false);               
                JOptionPane.showMessageDialog(this, "Data berhasil disimpan","Successfully !",JOptionPane.INFORMATION_MESSAGE);
            }catch(Exception e){
                System.err.println(e);
            }    
        }
    }

    private boolean cek() {
        boolean result;
        if(jTextField1.getText().equals("")||
           jTextField2.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Data error !","Error !",JOptionPane.ERROR_MESSAGE);
            result=false;
        }
        else
            result=true;
        return result;
    }

    private void enableAll(boolean b) {
        jTextField3.setEditable(!b);
        jTextField4.setEditable(!b);
        jTextField7.setEditable(!b);
        jTextField10.setEditable(!b);
        //deviasi.setVisible(!b);
    }
}
